package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.PipeZijinTjController;
import com.antoco.operation.bigscreen.model.dto.PipeZijinTjDTO;
import com.antoco.operation.bigscreen.model.entity.PipeZijinTj;
import com.antoco.operation.bigscreen.mapper.PipeZijinTjMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.PipeZijinTjService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/pipe-zijin-tj")
@RestController
public class PipeZijinTjControllerImpl implements PipeZijinTjController {

    @Autowired
    private PipeZijinTjService pipeZijinTjService;

    @Autowired
    private PipeZijinTjMapper pipeZijinTjMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public PipeZijinTjDTO save(@Validated @RequestBody PipeZijinTjDTO pipeZijinTjDTO) {
        PipeZijinTj pipeZijinTj = pipeZijinTjMapper.asEntity(pipeZijinTjDTO);
        pipeZijinTjService.save(pipeZijinTj);
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeZijinTjDTO;
    }

    @Override
    @GetMapping("/{id}")
    public PipeZijinTjDTO findById(@PathVariable("id") String id) {
        PipeZijinTj pipeZijinTj = pipeZijinTjService.findById(id).orElse(null);
        return pipeZijinTjMapper.asDTO(pipeZijinTj);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        pipeZijinTjService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<PipeZijinTjDTO> list() {
        return pipeZijinTjMapper.asDTOList(pipeZijinTjService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<PipeZijinTjDTO> pageQuery(Pageable pageable) {
        Page<PipeZijinTj> pipeZijinTjPage = pipeZijinTjService.findAll(pageable);
        List<PipeZijinTjDTO> dtoList = pipeZijinTjPage
                .stream()
                .map(pipeZijinTjMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, pipeZijinTjPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public PipeZijinTjDTO update(@RequestBody PipeZijinTjDTO pipeZijinTjDTO, @PathVariable("id") String id) {
        PipeZijinTj pipeZijinTj = pipeZijinTjMapper.asEntity(pipeZijinTjDTO);
        pipeZijinTjMapper.asDTO(pipeZijinTjService.update(pipeZijinTj, id));
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeZijinTjDTO;
    }
}